import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:smartidea_shop/utils/base.dart';

class SwiperBox extends StatefulWidget {
  late final List<Map> swiperData;
  SwiperBox(
      {Key? key,
      this.swiperData = const [
        {
          'type': 'img',
          'url': 'http://118.190.144.187/u-pages/static/images/ecd463.png'
        },
        {
          'type': 'img',
          'url': 'http://118.190.144.187/u-pages/static/images/ecd463.png'
        },
        {
          'type': 'img',
          'url': 'http://118.190.144.187/u-pages/static/images/ecd463.png'
        }
      ]})
      : super(key: key);

  @override
  _SwiperBoxState createState() => _SwiperBoxState();
}

class _SwiperBoxState extends State<SwiperBox> {
  List<Image> imgs = [];
  @override
  void initState() {
    widget.swiperData.forEach((element) {
      final imgUrl = element['url'];
      if (imgUrl != null) {
        final img = Image.network(imgUrl, fit: BoxFit.cover);
        imgs.add(img);
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
          width: 335,
          height: 335,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(14.0), //弧度
            child: Swiper(
              itemBuilder: (BuildContext context, int index) {
                return imgs[index];
              },
              pagination: SwiperPagination(),
              itemCount: imgs.length,
              autoplay: true,
            ),
          )),
    );
  }
}
